package org.ydsw.data.manage.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.ydsw.data.manage.entity.ScheduleJobLogEntity;
import org.ydsw.data.manage.service.ScheduleJobLogService;
import org.ydsw.data.manage.utils.UtilStrings;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: ScheduleJobLogServiceImpl </p>
 * <p>Description：
 * 任务日志保存接口实现类
 *
 * </p>
 * @author admin
 * @version 1.0.0
 */
@Service("scheduleJobLogService")
public class ScheduleJobLogServiceImpl implements ScheduleJobLogService {

    /**
     * 定义数据服务DAO
     */
    @Resource
    private JdbcTemplate mysqlJdbcTemplate;
    /**
     * 根据ID查询，返回数据对象
     * @param jobId 主键
     * @return
     */
    @Override
    public ScheduleJobLogEntity queryObject(Long jobId) {
        StringBuffer sql = new StringBuffer(1000);
        sql.append("select log_id,job_id,bean_name,method_name")
                .append(",params,status,error,times,create_time")
                .append(" from schedule_job_log")
                .append(" where log_id=").append(jobId);
        List<Map<String,Object>> list = mysqlJdbcTemplate.queryForList(sql.toString());
        if(list !=null && list.size()>0){
            return JSON.parseObject(JSON.toJSONString(list.get(0)), ScheduleJobLogEntity.class);
        } else {
            return null;
        }
    }

    /**
     * 根据map参数查询，返回list对象
     * @param paras 参数
     * @return
     */
    @Override
    public List<Map<String, Object>> queryList(Map<String, Object> paras) {
        StringBuffer sql = new StringBuffer(1000);
        sql.append("select log_id,job_id,bean_name,method_name")
                .append(",params,status,error,times,create_time")
                .append(" from schedule_job_log")
                .append(" where 1=1 " );
        //验证开始时间
        if(paras.get("sdate")!=null){
            sql.append(" and create_time>='")
                    .append(UtilStrings.mapToStringByKey(paras,"sdate"))
                    .append("'");
        }
        //验证结束时间
        if(paras.get("edate")!=null){
            sql.append(" and create_time <='")
                    .append(UtilStrings.mapToStringByKey(paras,"edate"))
                    .append("'");
        }
        //对数据进行排序
        sql.append(" order by log_id desc");
        if(paras.get("pageSize") != null && paras.get("page") != null){
            int rows = UtilStrings.mapToIntByKey(paras,"page")-1;
            int rowe = UtilStrings.mapToIntByKey(paras,"pageSize");
            sql.append(" limit ").append(rows * rowe).append(",").append(rowe);
        }
        return mysqlJdbcTemplate.queryForList(sql.toString());
    }

    /**
     * 根据map参数，返回数据总数
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        StringBuffer sql = new StringBuffer(1000);
        sql.append("select count(log_id) as rowCount from schedule_job_log");
        //对开始时间进行验证
        if(map !=null && map.get("sdate")!=null){
            sql.append(" and create_time>='")
                    .append(UtilStrings.mapToStringByKey(map,"sdate"))
                    .append("'");
        }
        //对结束时间进行验证
        if(map != null && map.get("edate")!=null){
            sql.append(" and create_time <='")
                    .append(UtilStrings.mapToStringByKey(map,"edate"))
                    .append("'");
        }
        //执行SQL语句
        List<Map<String, Object>> list = mysqlJdbcTemplate.queryForList(sql.toString());
        if(list != null && list.size()>0){
            return Integer.parseInt(UtilStrings.dealEmptyString(list.get(0).get("rowCount")));
        } else {
            return 0;
        }
    }

    /**
     * 保存数据对象
     * @param log 实体
     * @return
     */
    @Override
    public void save(ScheduleJobLogEntity log) {
        StringBuffer sql = new StringBuffer(1000);
        //定义SQL语句
        sql.append("insert into schedule_job_log(job_id,bean_name,method_name")
                .append(",params,status,error,times,create_time)")
                .append(" values(?,?,?,?,?,?,?,?)");
        //封装数据
        Object[] object = new Object[]{log.getJobId(),log.getBeanName()
                ,log.getMethodName(),log.getParams(),log.getStatus()
                ,log.getError(),log.getTimes(),log.getCreateTime()};
        //执行更新
        mysqlJdbcTemplate.update(sql.toString(),object);
    }

}
